/*****************************************************************************
* File Name: button.h
*
* Version: 1.0
*
* Description:
* This file contains API for reading the button status
*
* Note:
* Define DISABLE_BUTTON in platform.h to disable button module
* Define DISABLE_KEYBOARD_INTER_COLUMN_BUTTON in platform.h to disable buttons between the keyboard
*
* Owner: MRAO
* 
* Related Document:
* cy_pin component datasheet 
*
* Hardware Dependency:
* Buttons hardware
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#ifndef _BUTTON_H_
#define _BUTTON_H_

#include "platform.h"
#include "device.h"

#define NUMBER_OF_BUTTONS                     (1)
#define BUTTON_SCHMITT_HIGH_THRESH          (0xC0) /* 240 */
#define BUTTON_SCHMITT_LOW_THRESH           (0x40) /* 64  */
#define BUTTON_INCREMENT                    (0x3F)

/*
       C1  C2  C3
        |   |   |
    R1- o - o - o
        |   |   |
    R2- o - o - o
        |   |   |
    R3- o - o - o
        |   |   |
*/

#define KEYBOARD_ROWS_SENSOR_POSITION_1      (0x2)
#define KEYBOARD_ROWS_SENSOR_POSITION_2      (0x1)

#define KEYBOARD_PRESS_TIME_OUT              (0x200)
#define KEYBOARD_RELEASED_TIME_OUT           (0x200)

#define MOTION_SENSOR_KEYBOARD_POSITION_MASK (0x4)
#define VOICE_SENSOR_KEYBOARD_POSITION_MASK  (0x1)
#define RETURN_KEYBOARD_POSITION_MASK        (0x1)

/* Button Positon */
#define VOICE_BUTTON_MASK                     (0x1)
#define MOTION_SENSOR_BUTTON_MASK             (0x2)
#define RETURN_BUTTON_MASK                    (0x4)

#define VOICE_BUTTON_POSITION                 (0x0)

#define RETURN_KEY_CODE                       (0x9E)

#ifdef NEC_PROTOCOL
#define RETURN_IR_KEY_CODE                    (0x16)
#else
#define RETURN_IR_KEY_CODE                    (0x58)
#endif /* NEC_PROTOCOL */

/* Repeat button count in millisecond */
#define BUTTON_REPEAT_TIMEOUT                 (108u)

#ifndef DISABLE_BUTTON
/*****************************************************************************
* Function Name: Button_Init()
******************************************************************************
* Summary:
* This function is used to initialize button module
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Button_Init(void);

/*****************************************************************************
* Function Name: Button_Poll()
******************************************************************************
* Summary:
* This function is used to poll button status and report the status of the button
*
* Parameters:
* None
*
* Return:
* uint8 - status of the button
*****************************************************************************/
extern uint8 Button_Poll(void);

/*****************************************************************************
* Function Name: Button_Set_State()
******************************************************************************
* Summary:
* This function is used to set the device state and make corresponding configuration
* changes in button module
*
* Parameters:
* state - Sate of the button to be set.
*
* Return:
* None
*****************************************************************************/
extern void Button_Set_State(Device_State state);

/*****************************************************************************
* Function Name: Button_Get_Report()
******************************************************************************
* Summary:
* This function is used to update report with respect to the button status
*
* Parameters:
* report - structure to be filled
*
* Return:
* ReportType - type of the report that the data is filled 
*****************************************************************************/
extern ReportType Button_Get_Report(Report *report);

/*****************************************************************************
* Function Name: Button_IsActive()
******************************************************************************
* Summary:
* This function is used to check if any button is still pressed
*
* Parameters:
* None
*
* Return:
* bool - true if any button is pressed
*****************************************************************************/
extern bool Button_IsActive(void);

/*****************************************************************************
* Function Name: Button_Stop()
******************************************************************************
* Summary:
* This function is used to stop button module
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Button_Stop(void);

#else
#define Button_Init()
#define Button_Poll()                               0x00
#define Button_Set_State(state)
#define Button_Get_Report(report)                   NO_REPORT
#define Button_IsActive()                           false
#define Button_Stop()
#endif /* DISABLE_BUTTON */

#endif /* _BUTTON_H_ */
